<template>
  <div class="login-wrap">
    <div class="login-content">
      <img class="login-content-img" src="../assets/logo.png" />
      <el-form>
        <el-form-item>
          <el-input type="text" class="input" v-model="userName" placeholder="请输入用户账号" autocomplete="off"> <img
              class="input-icon" src="../assets/user.png" slot="prefix" />
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-input type="password" class="input" v-model="password" placeholder="请输入用户密码" autocomplete="off"><img
              class="input-icon" src="../assets/clockicon.png" slot="prefix" /></el-input>
        </el-form-item>
        <el-form-item v-if="registerStatus">
          <el-input type="password" class="input" v-model="password2" placeholder="请确认用户密码" autocomplete="off"><img
              class="input-icon" src="../assets/clockicon.png" slot="prefix" /></el-input>
        </el-form-item>
        <div class="login-text" v-if="!registerStatus">
          <span>还没有账号?</span>
          <span @click="back">立即注册</span>
        </div>
        <div class="login-text" v-if="registerStatus">
          <span></span>
          <span @click="back">返回登录</span>
        </div>
        <el-form-item v-if="!registerStatus">
          <el-button class="login-content-btn" type="primary" v-loading.fullscreen.lock="fullscreenLoading"
            @click="doLogin" style="width: 100%;">登录</el-button>
        </el-form-item>
        <el-form-item v-if="registerStatus">
          <el-button class="login-content-btn" type="primary" @click="register" style="width: 100%;">注册</el-button>
        </el-form-item>
      </el-form>

    </div>
  </div>
</template>

<script>
  import axios from 'axios'
  export default {
    name: 'Login',
    data: function () {
      return {
        fullscreenLoading: false,
        // userName: 'yxk',
        // password: 'yxk_yxk',
        userName: '',
        password: '',
        password2: '',
        registerStatus: false
      }
    },
    methods: {
      back () {
        this.registerStatus = !this.registerStatus
        this.userName = '',
          this.password = '',
          this.password2 = ''
      },
      doLogin: function () {
        window.sessionStorage.clear();
        this.fullscreenLoading = true;
        let userName = this.userName;
        let password = this.password;
        let url = this.baseUrl + 'login';
        let params = {
          userName: userName,
          password: password
        }
        //发起ajax请求-GET(注意参数必须保存到params属性中)
        axios.post(url, params)
          .then(resp => {
            this.$message({
              type: resp.data.code === 200 ? 'success' : 'error',
              message: resp.data.msg
            });
            //1.保存token到sessionStorage
            window.sessionStorage.setItem('token', resp.data.data.token)
            window.sessionStorage.setItem('role', resp.data.data.role)
            this.fullscreenLoading = false;
            //2.跳转路由
            this.$router.push('/home')
          })
          .catch(Error => {
            this.fullscreenLoading = false;
            this.$message({
              type: 'error',
              message: "网络异常"
            });
            console.log(Error)
          })
      },
      register () {
        let userName = this.userName;
        let password = this.password;
        let checkPassword = this.password2;
        let url = this.baseUrl + 'user/register';
        let params = {
          userName: userName,
          password: password,
          checkPassword: checkPassword,
        }
        //发起ajax请求-GET(注意参数必须保存到params属性中)
        axios.post(url, params)
          .then(resp => {
            this.$message({
              type: resp.data.code === 200 ? 'success' : 'error',
              message: resp.data.msg
            });
            this.back()
          })
          .catch(Error => {
            this.fullscreenLoading = false;
            this.$message({
              type: 'error',
              message: "网络异常"
            });
            console.log(Error)
          })
      }

    }
  }
</script>

<style scoped>
  .login-wrap {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100vh;
    background-color: #1ABC9C;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
  }


  .login-content {
    width: 330px;
    /* height: 360px; */
    background-color: #fff;
    border-radius: 5px;
    padding: 30px 30px;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    flex-direction: column;

  }

  .login-content-img {
    width: 80px;
    height: 80px;
    margin: 0 auto;
    margin-bottom: 20px;
  }

  .input-icon {
    width: 16x;
    height: 16px;
    margin-left: 5px;
  }

  .el-input__prefix img {
    position: absolute;
    top: 12px;
  }

  .login-text {
    margin-top: -10px;
    font-size: 13px;
    margin-bottom: 20px;
    text-align: right;
    color: #666;
  }

  .login-text span:last-child {
    color: #1ABC9C;
    margin-left: 10px;

  }
</style>